'use strict';

/**
 * @name HeaderCtrl
 * @author Yud
 */

angular.module('metalabOperationApp').controller('MessageCtrl', ['$scope', '$state', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaUtils', function($scope, $state, $stateParams, MetaResources, MetaRemind, MetaUtils) {
    $scope.listCtrl = {
        tableParams: {},
        demandNum: 0,
        copNum: 0,
        techNum: 0,
        solNum: 0
    };
    $scope.getMessages = function(page) {
        var range = ((page - 1) * 10 + 1) + "_" + (page * 10);
        MetaResources.message.query({
                'user_id': '_self',
                '_range': range,
                '_sort':'-created_at'
            },
            function(data, header) {
              MetaUtils.receiveListData($scope.listCtrl, $stateParams, page, data, header);
            },
            function(error) {
                MetaRemind.serverError(error);
            });
    };

    $scope.changeUrl = function() {
        var value = $scope.listCtrl.tableParams;
        $state.go('meta.permission.message', {
            page: value.currentPage
        });
    };
    $scope.init = function() {
        MetaUtils.initTableModule($scope.listCtrl.tableParams, $stateParams, $scope.changeUrl);
        $scope.getMessages(($stateParams.page !== undefined && parseInt($stateParams.page) > 0) ? $stateParams.page : 1);
        $scope.openModel = MetaUtils.openModel;
        MetaResources.user.get({
                'id': '_self'
            },
            function(data) {
                $scope.user = data;
            },
            function(error) {
                MetaRemind.serverError(error);
            });
        MetaResources.demand.query({
                '_range': 0,
                'projectManagerId': '_self',
                'tags_not': '合作'
            },
            function(data, header) {
                $scope.listCtrl.demandNum = header()['x-count'];
            },
            function(error) {
                MetaRemind.serverError(error);
            });
        MetaResources.demand.query({
                '_range': 0,
                'projectManagerId': '_self',
                'tags': '合作'
            },
            function(data, header) {
                $scope.listCtrl.copNum = header()['x-count'];
            },
            function(error) {
                MetaRemind.serverError(error);
            });
        MetaResources.technology.query({
                '_range': 0,
                'projectManagerId': '_self',
                'tags_not': '合作'
            },
            function(data, header) {
                $scope.listCtrl.techNum = header()['x-count'];
            },
            function(error) {
                MetaRemind.serverError(error);
            });
        MetaResources.technology.query({
                '_range': 0,
                'projectManagerId': '_self',
                'tags': '合作'
            },
            function(data, header) {
                $scope.listCtrl.solNum = header()['x-count'];
            },
            function(error) {
                MetaRemind.serverError(error);
            });
    };

    $scope.init();
}]);
